//
//  UserShopCarTable.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/12.
//

import Foundation
class UserShopCarTable{
    
    static func initTable(){
        let sqlite = SQLiteManager.sharedInstance
        
        if !sqlite.openDB(){return}
        
        let createSql = "CREATE TABLE IF NOT EXISTS userShopCar('userid'  TEXT,'bookid' TEXT);"
        if !sqlite.execNoneQuerySQL(sql: createSql){sqlite.closeDB() ; return}
        sqlite.closeDB()
        
    }

    static func InsertUserShopCar(usershopcar:ShopCar){
        
        let sqlite = SQLiteManager.sharedInstance
        
        if !sqlite.openDB(){return}
        
        let update = "INSERT OR REPLACE INTO userShopCar(userid,bookid) VALUES('"+usershopcar.userid+"','"+usershopcar.bookid+"');"
     
        if !sqlite.execNoneQuerySQL(sql: update){sqlite.closeDB() ; return}
        
        sqlite.closeDB()
    }
    
    static func GetUserShopCar(){
        
        let sqlite = SQLiteManager.sharedInstance
        
        if !sqlite.openDB(){return}
        
        let queryResult = sqlite.execQuerySQL(sql: "SELECT * FROM userShopCar")
        
        print(queryResult!)
        
        sqlite.closeDB() ;
    }
    static func getBookId(_ userid:String) -> [[String:Any]]?
    {
        let sqlite = SQLiteManager.sharedInstance
        sqlite.openDB()
        let queryuserid = sqlite.execQuerySQL(sql: "SELECT bookid FROM userShopCar WHERE userid='"+userid+"';")
        sqlite.closeDB();
        return queryuserid
    }
    static func deleteUserShopCar(_ id:String,_ userid:String)
    {
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let cleanAllStu = "DELETE FROM userShopCar WHERE bookid='"+id+"' AND userid='"+userid+"';"
        if !sqlite.execNoneQuerySQL(sql: cleanAllStu){sqlite.closeDB() ; return}

        sqlite.closeDB();
    }
    static func deleteUserShopCar(_ userid:String)
    {
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let cleanAllStu = "DELETE FROM userShopCar WHERE userid='"+userid+"';"
        if !sqlite.execNoneQuerySQL(sql: cleanAllStu){sqlite.closeDB() ; return}

        sqlite.closeDB();
    }
}
